//
//  CMDBAlbumManager.swift
//  CMDB
//
//  Created by 王磊 on 17/4/12.
//  Copyright © 2017年 CMDB. All rights reserved.
//

import UIKit

class CMDBAlbumManager: NSObject {
    
    static let getInstance: CMDBAlbumManager = CMDBAlbumManager()
    
    private override init() {
        super.init()
        
    }
}
extension CMDBAlbumManager: CMDBAlbumManagerDelegate {}

// MARK: 获取所有的相册
extension CMDBAlbumManager {
    
    func queryAlbums(albumParam: CMDBAlumbParam, handler: CMDBAlbumsHandler, error: CMDBHttpErrorHandler, failure: CMDBHttpFailureHandler) {
        
        CMDBHttpManager.getInstance.request(albumParam.URLString(), param: albumParam.keyValues, success: { [weak self] (sysTime, data, page) in
            
            let arr = CMDBAlbumsBean.objectArrayWithKeyValuesArray(data as! NSArray) as? Array<CMDBAlbumsBean>
            handler(albumsList: arr)
            
            if let arr = arr {
                
                self!.saveAlbums(arr, identifier: "CMDBAlbumCache_\(CMDBApplicationManager.getInstance.account!.uid)")
            }
            
            }, error: error, failure: failure)
        
    }
    func saveAlbums(albumList: Array<CMDBAlbumsBean>, identifier: String) {
        
        let mutable = NSMutableArray()
        
        _ = albumList.map {
            
            mutable.addObject($0.keyValues!)
        }
        
        CMDBCacheDataManager.getInstance.saveArray(mutable, identifier: identifier)
        
    }
    func queryAlbumsCache(identifier: String) throws -> Array<CMDBAlbumsBean> {
        
        guard let cache = CMDBCacheDataManager.getInstance.queryArray("CMDBAlbumCache_\(identifier)") else {
            
            printLog(CMDBAlbumControllerExceptionCodeType.AlbumsCacheEmpty.rawValue)
            
            throw CMDBAlbumControllerExceptionType.ExceptionCode(code: CMDBAlbumControllerExceptionCodeType.AlbumsCacheEmpty)
        }
        return CMDBAlbumsBean.objectArrayWithKeyValuesArray(cache) as! Array<CMDBAlbumsBean>
    }
}
// MARK: 获取相册的图片

extension CMDBAlbumManager {
    
    func queryPics(picsParam: CMDBPicParam, handler: CMDBPicsHandler, error: CMDBHttpErrorHandler, failure: CMDBHttpFailureHandler) {
        
        CMDBHttpManager.getInstance.request(picsParam.URLString(), param: picsParam.keyValues, success: { [weak self] (sysTime, data, page) in
            
            let arr = CMDBPicBean.objectArrayWithKeyValuesArray(data as! NSArray) as? Array<CMDBPicBean>
            handler(picsList: arr)
            
            if let arr = arr {
                
                self!.savePics(arr, identifier: "CMDBAlbumPicsCache_\(CMDBApplicationManager.getInstance.account!.uid)_\(picsParam.aid)")
            }
            
            }, error: error, failure: failure)
    }
    
    func savePics(picsList: Array<CMDBPicBean>, identifier: String) {
        
        let mutable = NSMutableArray()
        
        _ = picsList.map {
            
            mutable.addObject($0.keyValues!)
        }
        
        CMDBCacheDataManager.getInstance.saveArray(mutable, identifier: identifier)
    }
    func queryPicsCache(identifier: String) throws -> Array<CMDBPicBean> {
        
        guard let cache = CMDBCacheDataManager.getInstance.queryArray("CMDBAlbumPicsCache_\(identifier)") else {
            
            printLog(CMDBAlbumControllerExceptionCodeType.AlbumPicsCacheEmpty.rawValue)
            
            throw CMDBAlbumControllerExceptionType.ExceptionCode(code: CMDBAlbumControllerExceptionCodeType.AlbumPicsCacheEmpty)
        }
        return CMDBPicBean.objectArrayWithKeyValuesArray(cache) as! Array<CMDBPicBean>
    }
}

// MARK: 修改相册名称
extension CMDBAlbumManager {
    
    func modifyAlbumName(albumParam: CMDBAddAlumbParam, handler: CMDBModifyAlbumNameHandler, error: CMDBHttpErrorHandler, failure: CMDBHttpFailureHandler) throws {
        
        guard !albumParam.name.isEmpty else {
            
            printLog("\(CMDBAlbumControllerExceptionCodeType.AlbumNameEmpty.rawValue)")
            
            throw CMDBAlbumControllerExceptionType.ExceptionCode(code: CMDBAlbumControllerExceptionCodeType.AlbumNameEmpty)
        }
        
        CMDBHttpManager.getInstance.request(albumParam.URLString(), param: albumParam.keyValues, success: { (sysTime, data, page) in
            
            handler(isSuccess : true)
            
            }, error: error, failure: failure)
    }
}

// MARK: 创建相册

extension CMDBAlbumManager {
    
    func addAlbum(albumParam: CMDBAddAlumbParam, handler: CMDBAddAlbumHandler, error: CMDBHttpErrorHandler, failure: CMDBHttpFailureHandler) throws {
        
        guard !albumParam.name.isEmpty else {
            
            printLog("\(CMDBAlbumControllerExceptionCodeType.AlbumNameEmpty.rawValue)")
            
            throw CMDBAlbumControllerExceptionType.ExceptionCode(code: CMDBAlbumControllerExceptionCodeType.AlbumNameEmpty)
        }
        
        CMDBHttpManager.getInstance.request(albumParam.URLString(), param: albumParam.keyValues, success: { (sysTime, data, page) in
            
            let album = CMDBAlbumsBean.objectWithKeyValues(data as! NSDictionary) as! CMDBAlbumsBean
            
            handler(album : album)
            
            }, error: error, failure: failure)
        
    }
}

// MARK: 删除多个照片
extension CMDBAlbumManager {
    
    func deletePics(picsParam: CMDBDeletePicParam, handler: CMDBDeletePicsHandler, error: CMDBHttpErrorHandler, failure: CMDBHttpFailureHandler) throws {
        
        guard picsParam.tempName.count != 0 else {
            
            printLog("\(CMDBAlbumControllerExceptionCodeType.UnSelectedPics.rawValue)")
            
            throw CMDBAlbumControllerExceptionType.ExceptionCode(code: CMDBAlbumControllerExceptionCodeType.UnSelectedPics)
        }
        
        CMDBHttpManager.getInstance.request(picsParam.URLString(), param: ["name":picsParam.name], success: { (sysTime, data, page) in
            
            handler(isSuccess : true)
            
            }, error: error, failure: failure)
    }
}

